package com.android.killtencent.activity;

import android.content.Intent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.android.killtencent.BaseApp;
import com.android.killtencent.R;
import com.android.killtencent.task.LoginTask;
import com.android.pc.ioc.inject.InjectInit;
import com.android.pc.ioc.inject.InjectLayer;
import com.android.pc.ioc.inject.InjectView;
import com.android.pc.util.Handler_String;

/**
 * Created by rose on 15/2/9.
 */
@InjectLayer(value = R.layout.activity_welcome_layout, isFull = true)
public class WelcomActivity extends BaseActivity {
    @InjectView
    private RelativeLayout mLayout;
    private Animation mAnimaIn;
    private Animation mAnimaMiddle;
    private Animation mAnimaOut;

    private void startAnimation() {
        mLayout.startAnimation(mAnimaIn);
    }

    /*
    * 对所有动画进行实例化
    * */

    @InjectInit
    private void init() {
        mAnimaIn = AnimationUtils.loadAnimation(WelcomActivity.this, R.anim.welcome_fade_in);
        mAnimaIn.setDuration(1000);
        mAnimaMiddle = AnimationUtils.loadAnimation(WelcomActivity.this, R.anim.welcome_fade_in_scale);
        mAnimaMiddle.setDuration(3000);
        mAnimaOut = AnimationUtils.loadAnimation(WelcomActivity.this, R.anim.welcome_fade_out);
        mAnimaOut.setDuration(1000);
        startAnimation();
        setAnimationListener();
    }

    /*
    * 对动画进行监听
    * 实现循环放大变化
    */
    private void setAnimationListener() {
        mAnimaIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mLayout.startAnimation(mAnimaMiddle);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mAnimaMiddle.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mLayout.startAnimation(mAnimaOut);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        mAnimaOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                if (Handler_String.isNull(BaseApp.getInstance().getData("userName"))) {
                    Intent intent = new Intent(WelcomActivity.this, LoginActivity.class);
                    startActivity(intent);
                    onBackPressed();
                } else {
                    //如果本地已经存储有用户名，则取出进行尝试登陆，登陆失败再重新跳转到登陆界面
                    new LoginTask(BaseApp.getInstance().getData("userName"), BaseApp.getInstance().getData("passWord"), WelcomActivity.this).execute();
                }

            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

}
